<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
      label-width="68px">
      <!-- <el-form-item label="协议标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入协议标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="支付名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入支付名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate" v-hasPermi="['system:notice:edit']">修改</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="agreementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="支付名称" align="center" prop="name" :show-overflow-tooltip="true"
        width="160" />
      <el-table-column label="支付标识" align="center" prop="method" :show-overflow-tooltip="true"
        width="160" />
      <el-table-column label="状态" align="center" prop="enabled" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enabled" @change="handleEnabledChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="顺序" align="center" prop="orderNum" width="80" />
      <el-table-column label="背景图" prop="icon" :show-overflow-tooltip="true" width="100"
        align="center">
        <template slot-scope="scope">
          <el-image style="width: 50px" :src="scope.row.icon" :preview-src-list="scope.row.icon">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="账户" align="center" prop="account" width="200"
        :show-overflow-tooltip="true" />
      <el-table-column label="开户名称" align="center" prop="accountName" width="180"
        :show-overflow-tooltip="true" />
      <el-table-column label="银行名称" align="center" prop="bankName" width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="支行名称" align="center" prop="subbranch" width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:notice:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->

    <!-- 添加或修改支付方式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="支付名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入支付名称" />
        </el-form-item>
        <el-form-item label="支付标识" prop="method">
          <el-input v-model="form.method" placeholder="请输入支付标识" />
        </el-form-item>
        <el-form-item label="背景图" prop="icon" label-width="92px">
          <el-upload action="#" list-type="picture-card" :auto-upload="false"
            :on-remove="handleRemove" :on-preview="handlePictureCardPreview" :on-change="changeImg"
            :show-overflow-tooltip="true" v-model="form.icon">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img class="el-upload-list__item-thumbnail" :src="file.url" />
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete"
                  @click="handleRemove(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" :show-overflow-tooltip="true" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="账户" prop="account" label-width="77px"
          v-if="form.name === '线下公账交易' ? true:false">
          <el-input v-model="form.account" placeholder="请输入账户" />
        </el-form-item>
        <el-form-item label="开户名称" prop="accountName" v-if="form.name === '线下公账交易' ? true:false">
          <el-input v-model="form.accountName" placeholder="请输入开户名称" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName" v-if="form.name === '线下公账交易' ? true:false">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="支行名称" prop="subbranch" v-if="form.name === '线下公账交易' ? true:false">
          <el-input v-model="form.subbranch" placeholder="请输入支行名称" />
        </el-form-item>
        <el-form-item label="状态" label-width="77px">
          <el-radio-group v-model="form.enabled">
            <el-radio label="true">启用</el-radio>
            <el-radio label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPayment,
  addPayment,
  updatePayment,
  handleEnabledChange
} from "@/api/financeAdmin/paymentMethodAdmin";
import axios from "axios";
import { getToken } from "@/utils/auth";
export default {
  name: "PaymentMethodAdmin",
  dicts: ["mall_agreement_classify"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      //   total: 0,
      //支付表格数据
      agreementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        createBy: undefined,
        classify: undefined
      },
      // 表单参数
      form: {},
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      imageFileList: undefined,
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "multipart/form-data"
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "支付名称不能为空", trigger: "blur" }
        ],
        icon: [{ required: true, message: "图片不能为空", trigger: "change" }],
        account: [{ required: true, message: "账户不能为空", trigger: "blur" }],
        accountName: [
          { required: true, message: "开户名称不能为空", trigger: "blur" }
        ],
        bankName: [
          { required: true, message: "银行名称不能为空", trigger: "blur" }
        ],
        subbranch: [
          { required: true, message: "支行名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询支付方式列表 */
    getList () {
      this.loading = true;
      var that = this;
      listPayment(this.queryParams).then(response => {
        that.agreementList = response.data;
        that.total = response.total;
        that.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.noticeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加支付方式";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改支付方式";
    },
    /** 修改启用状态*/
    handleEnabledChange (row) {
      let text = row.enabled === true ? "启用" : "禁用";
      this.$modal
        .confirm('确认"' + text + '"吗？')
        .then(function () {
          return handleEnabledChange(row.id, row.enabled);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.enabled = !row.enabled;
        });
    },
    /** 提交按钮 */
    submitForm: function () {
      if (this.imageFileList) {
        const loading = this.$loading({
          lock: true,
          text: "上传中...",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        });
        let formData = new FormData();
        formData.append("file", this.imageFileList[0].raw);
        var that = this;
        axios
          .post(`${process.env.VUE_APP_BASE_API}/oss/common/upload`, formData, {
            headers: this.headers
          })
          .then(res => {
            if (res.status == 200) {
              let data = res.data;
              if (data.code == 200) {
                that.$modal.msgSuccess("上传成功");
                that.form.icon = data.data.url;
                if (that.form.id != undefined) {
                  updatePayment(that.form.id, that.form).then(response => {
                    that.$modal.msgSuccess("修改成功");
                    that.open = false;
                    that.getList();
                  });
                } else {
                  addPayment(that.form).then(response => {
                    that.$modal.msgSuccess("新增成功");
                    that.open = false;
                    that.getList();
                  });
                }
              } else {
                that.$message.error("上传文件失败" + res.data.msg);
              }
            }
          })
          .finally(res => {
            loading.close();
          });
        return;
      }
    },
    // 删除图片
    handleRemove (file, fileList) {
      const ul = document.querySelector(".el-upload-list--picture-card");
      ul.removeChild(ul.children[0]);
      ul.nextSibling.style.display = "block";
      console.log(file, fileList);
    },
    // 预览图片
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.raw.url;
      this.dialogVisible = true;
    },
    //   上传图片
    changeImg (file, fileList) {
      const ul = document.querySelector(".el-upload-list--picture-card");
      ul.nextSibling.style.display = "none";
      this.imageFileList = fileList;
      console.log(file, this.imageFileList);
    }
  }
};
</script>
